﻿using RXY_Mall.Core;
using RXY_Mall.Data;
using RXY_Mall.Domain;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;

namespace RXY_Mall.Services
{
    public class GoodsService
    {
        private RXY_MallContext db = new RXY_MallContext();

        /// <summary>
        /// 是否可购买该商品
        /// </summary>
        /// <param name="goods_id"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool IsBuyGoods(int goods_id, out string error)
        {
            bool flag = false;
            error = "";
            var goods = db.gw_goods.Include(g => g.gw_shop)
                .Include(g => g.gw_goods_property)
                .FirstOrDefault(g => g.id == goods_id);

            if (goods == null || goods.deleted == true || goods.gw_goods_property.published != 1)
            {
                error = "商品已下架";
                return flag;
            }

            if (goods.gw_shop.status == 0 || goods.gw_shop.deleted)
            {
                error = "店铺已关闭";
                return flag;
            }

            if (goods.gw_goods_property.is_sale == false)
            {
                error = "无货";
                return flag;
            }

            return true;
        }

        /// <summary>
        /// 是否可以购买SKU商品
        /// </summary>
        /// <param name="sku_id"></param>
        /// <param name="quantity"></param>
        /// <param name="stock_manage_type"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool IsBuySku(int sku_id, int quantity, int stock_manage_type,  out string error)
        {
            bool flag = false;
            error = "";
            var sku = db.gw_goods_skus.Find(sku_id);

            if (sku == null || sku.deleted == true)
            {
                error = "商品已下架";
                return flag;
            }

            if (!sku.is_sale)
            {
                error = "暂无货";
                return flag;
            }

            if (sku.limit_quantity != null)
            {
                if (sku.limit_quantity < quantity)
                {
                    error = string.Format("该商品最多可买{0}", sku.limit_quantity);
                    return flag;
                }
            }

            if (stock_manage_type == 2)
            {
                if (sku.stock_quantity == null)
                {
                    error = "暂无货";
                    return flag;
                }

                if (sku.stock_quantity < quantity)
                {
                    error = string.Format("该商品最多可买{0}", sku.stock_quantity);
                    return flag;
                }
            }

            return true;
        }

        public bool ReduceSku(int sku_id, int quantity, out string error)
        {
            error = "";
            var flag = false;

            using (RXY_MallContext db = new RXY_MallContext())
            {
                //减少库存   
                lock (AppConfig.CurrentAmount)
                {
                    var goods_sku = db.gw_goods_skus.Find(sku_id);
                    if (goods_sku == null && goods_sku.deleted && !goods_sku.is_sale)
                    {
                        error = "该商品已下架";
                        return flag;
                    }

                    if (goods_sku.stock_quantity <= 0)
                    {
                        error = "该商品已售完";
                        return flag;
                    }

                    if (goods_sku.stock_quantity - quantity < 0)
                    {
                        error = "库存仅剩" + goods_sku.stock_quantity;
                        return flag;
                    }
                    LogHelper.WriteLog(string.Format("sku:{0},原库存{1},减少{2}", sku_id, goods_sku.stock_quantity, quantity));
                    goods_sku.stock_quantity -= quantity;
                    db.Entry(goods_sku).Property(p => p.stock_quantity).IsModified = true;

                    if (goods_sku.stock_quantity == 0)
                    {
                        goods_sku.is_sale = false;
                        db.Entry(goods_sku).Property(p => p.is_sale).IsModified = true;
                    }
                    db.SaveChanges();
                    flag = true;
                }
            }

            return flag;
        }

        public bool AddSku(int sku_id, int quantity, out string error)
        {
            error = "";
            var flag = false;
            try
            {
                using (RXY_MallContext db = new RXY_MallContext())
                {
                    //增加库存   
                    lock (AppConfig.CurrentAmount)
                    {
                        var goods_sku = db.gw_goods_skus.Find(sku_id);
                        if (goods_sku == null)
                        {
                            error = "该商品已下架";
                            return flag;
                        }
                        LogHelper.WriteLog(string.Format("sku:{0},原库存{1},增加{2}", sku_id, goods_sku.stock_quantity, quantity));
                        goods_sku.stock_quantity += quantity;
                        db.Entry(goods_sku).Property(p => p.stock_quantity).IsModified = true;

                        if (!goods_sku.is_sale)
                        {
                            goods_sku.is_sale = true;
                            db.Entry(goods_sku).Property(p => p.is_sale).IsModified = true;
                        }
                        db.SaveChanges();
                        flag = true;
                    }
                }
            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(ex);
            }

            return flag;
        }
    }
}
